﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    [Authorization]
    public class CommodityGiftController : BaseController
    {
        private readonly CommodityGiftBLL _bll = new CommodityGiftBLL();

        #region 列表

        public ActionResult List()
        {
            ViewBag.IsCreate = MagModules.FindAll(t => t
            .Action == "Create" && t
                .Controller == "CommodityBrand").Count > 0 ? 1 : 0;
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            int order;
            int.TryParse(Request["order[0][column]"], out order);
            string direction = Request["order[0][dir]"];
            var orderByColumns = new[] { "GiftCommodityName", "BelongToCommodityName", "GiftQuantity", "LeftQuantity" };
            string search = Request["search[value]"];
            int pageIndex = (start/length) + 1;

            TableDataViewDto<ViewCommodityGiftDto> theList = _bll.GetPagingList(this
                .MagModules, draw, pageIndex, length, SqlSafe(search),
                orderByColumns[order] + " " + direction);

            JsonResult jsonResult = Json(theList);
            jsonResult.MaxJsonLength = int.MaxValue;

            return jsonResult;
        }

        #endregion

        #region 创建

        public ActionResult Create()
        {
            InitiSet();
            ViewBag.SeletTimeSpanType = new List<SelectListItem>
            {
                new SelectListItem {Value = "0", Text = "日"},
                new SelectListItem {Value = "1", Text = "周"},
                new SelectListItem {Value = "2", Text = "月"},
                new SelectListItem {Value = "3", Text = "年"},
            };
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection, CommodityGift model)
        {
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            var commodityLibInfo = CommodityLibBLL.Instance.GetModel(model.BelongToCommodityLibId);
            if (commodityLibInfo == null || commodityLibInfo.Id == 0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            int result = _bll.Add(model);

            if (result > 0)
            {
                commodityLibInfo.HasGiftCommodity = true;
                commodityLibInfo.GiftCount++;
                commodityLibInfo.GiftEndTime = Convert.ToDateTime("2049-12-31 23:59:59");
                CommodityLibBLL.Instance.Update(commodityLibInfo);
                return RedirectToAction("List", "CommodityGift");
            }
            
            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 修改

        public ActionResult Update(int id)
        {
            InitiSet();

            if (id <= 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            CommodityGift model = _bll.GetModel(id);
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            ViewBag.SeletTimeSpanType = new List<SelectListItem>
            {
                new SelectListItem {Value = "0", Text = "日"},
                new SelectListItem {Value = "1", Text = "周"},
                new SelectListItem {Value = "2", Text = "月"},
                new SelectListItem {Value = "3", Text = "年"},
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(FormCollection collection, CommodityGift model)
        {
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            bool result = _bll.Update(model);

            if (result)
                return RedirectToAction("List", "CommodityGift");

            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 删除

        [HttpPost]
        public JsonResult Delete()
        {
            string ids = Request["doIds"];
            bool result = _bll.BatchDeleteList(ids);
            return Json(result);
        }

        #endregion

        #region Private Functions 自用函数

        private void InitiSet()
        {
            List<SelectListItem> lsCommodities =
                CommodityLibBLL.Instance.GetModelList().Select(model => new SelectListItem
                {
                    Value = model.Id.ToString(),
                    Text = model.CommodityName
                }).ToList();
            ViewBag.Commodities = lsCommodities;
        }
        #endregion
    }
}